package com.umbrella.manage.utils;

import java.util.UUID;

/**
 *
 *
 * @author Wan, ChuanLong @ 07/13/2018
 */
public class TraceUtils {

	private static final ThreadLocal<String> traceIdCache = new ThreadLocal<String>();

	public static String getTraceId(Object... items) {
		
		String traceId = traceIdCache.get();
		if(traceId == null) {
			
			setTraceId();
		}
		
		if(items == null || items.length == 0) {
			
			return traceIdCache.get();
		}
		
		StringBuilder sb = new StringBuilder(traceIdCache.get());
		for(Object item : items) {
			
			sb.append(",").append(item);
		}
		return sb.toString();
	}

	protected static void setTraceId() {
		traceIdCache.set(generateTraceId());
	}

	public static void clear() {
		traceIdCache.remove();
	}

	protected static String generateTraceId() {

		return UUID.randomUUID().toString().substring(24);
	}
}
